const jwt = require('jsonwebtoken');
const helper = require('../../util/helper');
const config = require('../../config/environment/index');
const mysql = require('../../core/mysql');

const log = require('../../core/logger').getLogger("system");
const moment = require('moment');
const common = require('../../core/common');
const stringUtils = require('../../core/util/StringUtils');
const _ = require('lodash');

exports.add = async(ctx) => {
  let model_name = ctx.request.body.model_name || '';
  let model_type = ctx.request.body.model_type || '';
  let model_property = ctx.request.body.model_property || '';
  let create_time = ctx.request.body.create_time || new Date();
  let create_user_id = ctx.user.id;

  let newModel = {
    model_name,
    model_type,
    model_property,
    create_time,
    create_user_id
  };
  let modelData = await mysql.query(`INSERT INTO model SET ?`, newModel);
  ctx.body = {
    results: 200,
    projectID: modelData.insertId,
    message: "新增model成功"
  };
}

exports.getList = async(ctx) => {
  let params = ctx.query;
  let counts = params.counts || 10;
  let indexfrom = params.indexfrom || 0;
  let results = await mysql.query(`SELECT * FROM model  ORDER BY model_id DESC LIMIT ${indexfrom}, ${counts}` );
  ctx.body = {
    result: 200,
    list:results,
    message: "succses"
  };
}

exports.update = async(ctx) => {
  let request_body = ctx.request.body;
  let model_id = request_body.model_id;
  let model_name = request_body.model_name || '';
  let model_type = request_body.model_type || '';
  let model_property = request_body.model_property || '';
  let results = await mysql.query(`UPDATE model SET model_name = '${model_name}', model_type = ${model_type}, model_property = '${model_property}' WHERE model_id = ${model_id}`);

  if (results.length > 0 || results){
    let newResult = await mysql.query(`select * from  model WHERE model_id = ${model_id}`);
    ctx.body = {
       result: 200,
      resultInfo: newResult,
      message: "更新model成功"
    };
  }
  else {
    ctx.body = {
      result: 0,
      message: "更新失败"
    };
  }
}

exports.delete = async(ctx) => {
  let request_body = ctx.request.body;
  let model_id = request_body.model_id;

  let results = await mysql.query(`delete  from model  WHERE model_id = ${model_id}`);

  if (results.length > 0 || results){
    let newResult = await mysql.query(`select * from  model`);
    ctx.body = {
        result: 200,
      list: newResult,
      message: "更新model成功"
    };
  }
  else {
    ctx.body = {
        result: 0,
      message: "更新失败"
    };
  }
}

